// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    wordCount:0,
    currentId:0,
    myRank:1,
    msgList:[],
    showModal: false,
    selectedItem: null,
    selectedTemItem: null,
    booksList:null,
    nickname: "Damonwxy",
    userId:"95e99f4d456511eea9ec0242ac110002",
    amount: 0

  },
  createRank() {
    wx.request({
      url: 'http://47.95.223.130:8083/Rank/add',
      data: {
        nickname: this.data.nickname
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: "POST",
      success: res => {
        console.log(res);
      },
      fail: res => {
        reject('失败啦')
      }
    })
},
  countTap:function(e) {
    this.setData({
      currentId:e.currentTarget.dataset.num
    })
    

  },
  start:function (e) {
    this.createRank();
    if(this.data.currentId == 0) {
      wx.setStorageSync('wordAmount', this.data.wordCount);
    } else {
      
    }
  },
  toRank:function(e) {
    wx.navigateTo({
      url: '/pages/rank/rank',
    })
  },
  upTap:function(e) {
    this.setData({
      wordCount:this.data.wordCount+10
    })
  },
  downTap:function(e) {
    this.setData({
      wordCount:this.data.wordCount-10
    })
    if(this.data.wordCount<0) {
      this.setData({
        wordCount:0
      })
    }
  },
  getRank(day) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: 'http://47.95.223.130:8083/Rank/query',
        data: {
          day: day
        },
        method: "GET",
        success: res => {
          console.log(day);
          resolve(res)
        },
        fail: res => {
          reject('失败啦')
        }
      })
    })
  },
  getWordsAounts() {
    return new Promise((resolve, reject) => {
      wx.request({
        // dataType: "其他",
        url: 'http://47.95.223.130:8083/Exam/request',
        data: {
          userId: this.data.userId,
        },
        method: "GET",
        success: res => {
          console.log(res);
          this.setData({
            amount: res.data.data.length
          })
          resolve(); // 请求成功后解析Promise
        },
        fail: res => {
          reject('失败啦')
        }
      })
    })
  },
  onLoad() {
    // var nickname = "qqqq";
    // var password = "qqqq";
    // wx.request({
    //   url: 'http://47.95.223.130:8083/User/login',
    //   method: "POST",
    //    data: {
    //     nickname: nickname,
    //     password: password
    //    },
    //   success: (res) => {
    //     console.log(res);
    //   },
    //   fail: (err) => {
    //     console.log(res);
    //   },
    //   complete: (res) => {},
    // })
    this.getWordsAounts();
    wx.request({
      url: 'http://47.95.223.130:8083/Book/query',
       data: "",
      success: (res) => {
        console.log(res);
        this.setData({
          booksList: res.data.data 
        })
        console.log(this.data.booksList);
      },
      fail: (err) => {},
      complete: (res) => {},
    })

    this.getRank(0).then(res => {	
      this.setData({
        msgList:res.data.data.reverse() 
      })
      console.log(res);
      var that = this;
      res.data.data.forEach(function(item, index) {
        if(item.users_nickname == that.data.nickname) {
          that.setData({
            myRank:index + 1
          })
          return;
        }
      });
      //console.log(this.data.msgList);
      this.createRank();
      
    }).catch(res => {
      console.log(res);
    })





    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
  },
  showModal_click: function () {
    this.setData({
      showModal: true
    })
  },
  //弹窗事件
  hideModal: function () {
    this.setData({
      showModal: false
    });
  },
  onCancel: function () {
    this.hideModal();
  },
  onConfirm: function () {
    if (this.data.selectedItem) {
      console.log("选中的内容：", this.data.selectedItem);
      
    }
    this.setData({
      selectedItem:this.data.selectedTenItem
    })
    this.hideModal();
  },
  selectItem: function (e) {
    var index = e.currentTarget.dataset.index;
    var selectedTenItem = this.data.booksList[index];
    this.setData({
      selectedTenItem: selectedTenItem
    });
  }
})
